<template>
  <div class='container'>
    <el-card>
      <el-form label-width="80px" :inline="true">
          <el-form-item label="关键字" class="form-input">
            <el-input v-model="keyword" placeholder="根据编号搜索"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="clearInput">
              清除
            </el-button>
            <el-button size="small" type="primary" @click="RandomsList()">
              搜索
            </el-button>
          </el-form-item>
      </el-form>
      <el-alert :closable="false" type="info" show-icon>
          <span slot="title">数据一共{{ total }}条</span>
      </el-alert>
      <el-table class="tableTab" :data="Randomslist">
        <el-table-column
        prop="id"
        label="编号"
        width="200">
        </el-table-column>
        <el-table-column
        prop="questionType"
        label="题型">
        <template slot-scope="scope">
          <span v-if="scope.row.questionType == 1">单选</span>
          <span v-if="scope.row.questionType == 2">多选</span>
          <span v-if="scope.row.questionType == 3">简答</span>
        </template>
        </el-table-column>
        <el-table-column
        label="题目编号"
        width="200">
        <template slot-scope="scpoe">
          <div v-for="(item, i) in scpoe.row.questionIDs" :key="i">
            <a href="JavaScript:;" class="Aclic" @click="openTab(item)">
              {{item.number}}
            </a>
          </div>
        </template>
        </el-table-column>
        <el-table-column
        label="录入时间"
        prop="addTime"
        width="180">
        </el-table-column>
        <el-table-column
        label="答题时间(s)"
        prop="totalSeconds"
        width="180">
        </el-table-column>
        <el-table-column
        label="正确率(%)"
        prop="accuracyRate"
        width="180">
        </el-table-column>
        <el-table-column
        label="录入人"
        prop="userName"
        width="180">
        </el-table-column>
        <el-table-column
        label="操作">
        <template slot-scope="scope">
            <el-button
              @click="deleteQuestion(scope.row)"
              type="danger"
              icon="el-icon-delete"
              circle
              size="small"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="fenye">
        <el-pagination
          style="margin-top: 20px;text-align: right"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page" 
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="total">
       </el-pagination>
      </el-row>
      <el-row>
        <el-dialog
          v-if="dialogVisible"
          title="题目预览"
          :visible.sync="dialogVisible"
          width="60%">
          <questions-preview :receive = RandomslistId></questions-preview>
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="dialogVisible = false">关 闭</el-button>
          </span>
        </el-dialog>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
import QuestionsPreview from '../components/questions-preview'
export default {
  props: {},
  components: {
    QuestionsPreview
  },
  data () {
    return {
      input: '',
      Randomslist: [],
      total: 0,
      page: 1,
      pagesize: 20,
      keyword: '',
      dialogVisible: false,
      RandomslistId: {}
    }
  },
  created () {
    this.RandomsList()
  },
  computed: {},
  methods: {
    async RandomsList () {
      try {
        const { data } = await randoms({page: this.page, pagesize:this.pagesize, keyword: this.keyword})
        this.Randomslist = data.items
        console.log(data)
        this.total = data.counts
      } catch (err) {
        this.$message('获取数据失败,请稍后重试')
      }
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.RandomsList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.RandomsList()
    },
    clearInput () {
      this.keyword = ''
      this.RandomsList()
    },
    async deleteQuestion (arr) {
      try {
        const { res } = await removeRandoms(arr)
        this.RandomsList()
        this.$message.success('删除成功')
      } catch (err) {
        this.$message.error('删除失败,请稍后重试')
      }
    },
    openTab (obj) {
      this.dialogVisible = true
      this.RandomslistId = obj
    }
  }
}
</script>

<style scoped lang='less'>
.el-form {
  display: flex;
  .form-input {
    flex: 1;
  }
  .el-input__inner {
    height: 30px;
  }
}
.tableTab{
  margin-top: 15px;
}
.Aclic{
  color: rgb(0, 153, 255);
}
.el-button--default{
  color: #f56c6c;
  background: #fef0f0;
  border-color: #fbc4c4;
}
.fenye{
  margin-right: 0;
}
</style>
